package com.zkzx.wlhy.adaptor.service.order;

import com.zkzx.common.company.model.CompanyVO;
import com.zkzx.common.freight.command.OrderCommand;
import com.zkzx.common.freight.constants.OrderStatusEnum;
import com.zkzx.common.freight.constants.PackageEnum;
import com.zkzx.common.freight.constants.ProductTypeEnum;
import com.zkzx.common.freight.model.OrderItemVO;
import com.zkzx.common.freight.model.OrderPayableVO;
import com.zkzx.common.freight.model.OrderVO;
import com.zkzx.util.date.DateUtil;
import com.zkzx.util.json.JSONResultUtil;
import com.zkzx.wlhy.adaptor.clinet.ResultVO;
import com.zkzx.wlhy.adaptor.clinet.order.WhOrderFeign;
import com.zkzx.wlhy.adaptor.controller.order.param.OrderDTO;
import com.zkzx.wlhy.adaptor.controller.order.vo.WhOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年05月21日 18时21分
 */
@Service
public class OrderService {

    @Autowired
    private WhOrderFeign whOrderFeign;

    public OrderCommand add(Integer id, OrderVO oder){
        OrderDTO orderDTO = new OrderDTO();
        CompanyVO company = oder.getCompanyVO();
        orderDTO.setCompanyId(company.getThreeId());
        orderDTO.setCreditCode(company.getCode());
        orderDTO.setWaybillNum(oder.getCid());
        orderDTO.setTotalFreight(oder.getOrderPayableVO().getFare().toString());
        orderDTO.setCreateTime(DateUtil.convertDateToString(oder.getCreateTime()));
        orderDTO.setCarriageContractId(oder.getInsuranceCode());
        orderDTO.setCompanyContractId(company.getContractCode());
        orderDTO.setNavigation(JSONResultUtil.list2JSON(oder.getOrderLogisticss()));
        orderDTO.setDriverId(oder.getDriverVO().getThreeId());
        orderDTO.setIdCard(oder.getDriverVO().getIdentification());
        orderDTO.setCarId(oder.getTruckVO().getThreeId());
        orderDTO.setPlateNumber(oder.getTruckVO().getHeaderLicenseVO().getLicence());

        OrderItemVO orderItem = oder.getOrderItemVO();

        List<OrderDTO.PushOrder> pushOrders = new ArrayList<>();

        OrderDTO.OrderDelivery consigner = OrderDTO.OrderDelivery.builder()
                .companyName(oder.getOrderDeliveryVO().getShipperCode())
                .name(oder.getOrderDeliveryVO().getShipperName())
                .telephone(oder.getOrderDeliveryVO().getShipperContact())
                .province(oder.getOrderDeliveryVO().getLoadProvince())
                .city(oder.getOrderDeliveryVO().getLoadCity())
                .county(oder.getOrderDeliveryVO().getLoadCounty())
                .detailAddress(oder.getOrderDeliveryVO().getLoadAddress())
                .build();

        OrderDTO.OrderDelivery receiving = OrderDTO.OrderDelivery.builder()
                .companyName(oder.getOrderDeliveryVO().getConsigneeCode())
                .name(oder.getOrderDeliveryVO().getConsigneeName())
                .telephone(oder.getOrderDeliveryVO().getConsigneeContact())
                .province(oder.getOrderDeliveryVO().getConsigneeProvince())
                .city(oder.getOrderDeliveryVO().getConsigneeCity())
                .county(oder.getOrderDeliveryVO().getConsigneeCounty())
                .detailAddress(oder.getOrderDeliveryVO().getConsigneeAddress())
                .build();


        OrderDTO.PushOrder pushOrder = OrderDTO.PushOrder.builder()
                .goodsName(orderItem.getProductName())
                .goodsWeight(orderItem.getProductWeight())
                .goodsPackage(PackageEnum.valueOf(orderItem.getProductPackag()).getValue())
                .goodsCategory(ProductTypeEnum.valueOf(orderItem.getProductSort()).getValue())
                .escortPicture(oder.getEscortPic())
                .loadingPicture(oder.getLoadPic())
                .signPicture(oder.getSignPic())
                .unloadingPicture(oder.getDischargePic())
                .loadingPictureTime(DateUtil.convertStringDateToStringDateTime(oder.getOrderDeliveryVO().getLoadTime()))
                .completeSignTime(DateUtil.convertStringDateToStringDateTime(oder.getOrderDeliveryVO().getConsigneeTime()))
                .consigner(consigner)
                .receiving(receiving)
                .build();
        pushOrders.add(pushOrder);
        orderDTO.setPushOrders(pushOrders);


        ResultVO<WhOrderVO> result = whOrderFeign.add(orderDTO);
        WhOrderVO vo = null;
        if (HttpStatus.OK.value() == result.getCode()){
            vo = result.getData();
            OrderPayableVO orderPayableVO = new OrderPayableVO();
            orderPayableVO.setTotalCost(vo.getTotalFreight());
            orderPayableVO.setKeepFareproPortion(vo.getTotalFreight().toString());
            return OrderCommand.builder()
                    .id(id)
                    .code(vo.getWaybillId())
                    .orderPayableVO(orderPayableVO)
                    .status(OrderStatusEnum.get(vo.getIsInvoice()).name())
                    .threeId(vo.getWaybillId())
                    .build();
        }

        return OrderCommand.builder()
                .id(id)
                .status(OrderStatusEnum.VERIFY_SUCCESS.name())
                .reason(result.getMessage())
                .build();
    }

}
